package com.tavenli.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.tavenli.entity.F18Device;

@Repository
public class F18DeviceDao extends BaseDao {
	private static Logger logger = LoggerFactory.getLogger(UserDao.class);
	public F18Device getF18DeviceById(int id){
		try {
			return this.getById(F18Device.class, id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
	public F18Device getSingleF18Device(String deviceSn) throws Exception{
		String hql = "from F18Device where deviceSn=?";
		TypedQuery<F18Device> query = this.getEntityManager().createQuery(hql, F18Device.class);
		query.setParameter(1, deviceSn);
		return query.getSingleResult();
	}
	
	public boolean existF18Device(String deviceSn) {
		String hql = "select count(*) from F18Device where deviceSn=?";
		Query query = this.createQuery(hql);
		query.setParameter(1, deviceSn);
		Object obj = query.getResultList().get(0);
		Long count = Long.parseLong(obj.toString());
		return count.compareTo(0L)>0;
	}
	public int queryDataCount(Map<String, Object> paramMap){
		StringBuilder hql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();
		
		hql.append("select count(*) from F18Device where 1=1");
		
		if(paramMap.get("id")!=null){
			hql.append(" and id=:id");
			params.put("id", paramMap.get("id"));
		}
		
		if(paramMap.get("deviceStatus")!=null){
			hql.append(" and deviceStatus=:deviceStatus");
			params.put("deviceStatus", paramMap.get("deviceStatus"));
		}
		
		return super.queryPageTotalCount(hql.toString(),params).intValue();
	}
	
	public List<F18Device> queryPageData(int start, int maxSize,Map<String, Object> paramMap){
		StringBuilder hql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();
		
		hql.append("from F18Device  where 1=1");
		
		if(paramMap.get("id")!=null){
			hql.append(" and id=:id");
			params.put("id", paramMap.get("id"));
		}
		
		if(paramMap.get("deviceSn")!=null){
			hql.append(" and deviceSn like:deviceSn");
			params.put("deviceSn", "%"+ paramMap.get("deviceSn") +"%");
			
		}
		
		if(paramMap.get("deviceStatus")!=null){
			hql.append(" and deviceStatus=:deviceStatus");
			params.put("deviceStatus", paramMap.get("deviceStatus"));
		}
		
		hql.append(" order by id desc");
		
		return super.queryPageList(hql.toString(), params, start, maxSize);
	}
	
public int delDevice(int deviceId){
		
		String hql = "delete F18Device t where t.id=?";
		
		Query query = this.getEntityManager().createQuery(hql);
		query.setParameter(1, deviceId);
		try {
			return query.executeUpdate();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return -1;
	}
	
	public int delDevices(Integer[] deviceIds){
		
		String hql = "delete F18Device t where t.id in ("+StringUtils.join(deviceIds,",")+")";
		
		Query query = this.getEntityManager().createQuery(hql);
				
		try {
			return query.executeUpdate();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return -1;
	}
}
